package com.test.controller;


import com.test.pojo.DinnerTable;
import com.test.service.DinnerTableService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class DinnerTableCtrl {
    @Resource
    DinnerTableService dinnerTableService;

    /**
     *             获取所有餐桌列表
     * @return
     */
    @RequestMapping("/getAllDinnerTable")
    public String getAllDinnerTable(Model model){
        List<DinnerTable> dinnerTableList = dinnerTableService.getAllDinnerTable();
        model.addAttribute("dinnerTableList",dinnerTableList);
        return "dinnerTableList";
    }

    /**
     *        添加餐桌页面
     * @return
     */
    @RequestMapping("/gotoAddDinnerTable")
    public String gotoAddDinnerTable(){
        return "DinnerTableAdd";
    }

    @PostMapping(value ="/doAddDinnerTable")
    public String doAddDinnerTable(DinnerTable dinnerTable,Model model){

        int num = dinnerTableService.addDinnerTable(dinnerTable);
        if(num > 0){
            model.addAttribute("msg","餐桌【"+dinnerTable.getTableName()+"】添加成功！");
        }else{
            model.addAttribute("msg","餐桌添加失败！");
        }
        return "forward:/getAllDinnerTable";
    }

    /**
     *          更改支付状态
     * @return
     */

    @RequestMapping(value = "/editDinnerTableStatus")
    public String editDinnerTableStatus(DinnerTable dinnerTable){
        dinnerTableService.updateDinnerTableStatus(dinnerTable);
        return "redirect:getAllDinnerTable";
    }


    /**
     *      删除餐桌
     */
    @RequestMapping(value ="/delDinnerTable/{id}",method = RequestMethod.GET)
    public String delFoodType(@PathVariable("id") int id,Model model){
        int num = dinnerTableService.deleteDinnerTable(id);
        if(num > 0){
            model.addAttribute("msg","餐桌删除成功！");
        }else if (num == -1){
            model.addAttribute("msg","餐桌已预定，所以不能删除该餐桌");
        }else{
            model.addAttribute("msg","餐桌删除失败！");
        }
        return "forward:/getAllDinnerTable";
    }
}
